package com.base.cn.platform.os.controller.web.qa.reply;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.qa.qareply.QaReplyService;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 前台问题评论Controller
 *
 * @author LIZHIPENG
 * @create 2018-05-26 下午 1:50
 */
@Controller
@RequestMapping("/web/uc/qa/reply")
public class WebQaReplyController extends WebBaseController {

    @Autowired
    private QaReplyService qaReplyService;
    @Autowired
    private QaQuestionService qaQuestionService;

    /**
     * 保存评论方法
     * @param request
     * @param params
     * @return Map<String, Object>
     */
    @RequestMapping("/saveQaReply")
    @ResponseBody
    public Map<String, Object> saveQaReply(HttpServletRequest request,
                                           @RequestBody Map<String, Object> params){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        //检查问题状态是否可回复
        if(ObjectUtils.isNotEmpty(params.get("questionId"))){
            BigDecimal questionId = new BigDecimal(params.get("questionId").toString());
            Map<String, Object> question = qaQuestionService.findQuestionByIdForFront(questionId, userId,false,true,true,true,true,true, false);
            if(!question.get("status").toString().equals("1")){
                return ResultUtil.ERROR("不可进行评论");
            }
            if(!(boolean)question.get("questioner") && !(boolean)question.get("attender") &&  !question.get("isFree").toString().equals("1")){
                return ResultUtil.ERROR("围观该问题后才可以评论");
            }
        }
        params.put("userId", userId);
        return qaReplyService.saveQaReply(params);
    }
}
